import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'interBankBasicInfoM',

  state: {
    getInfo: {},
    getSpeed: [],
    posisionData: { columns: [], dataSource: [] },
    BondInfo: {},
    currentUser: {},
  },

  effects: {
    //获取用户信息
    *getUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            currentUser: response.data,
          },
        });
        callback(response.data);
      }
    },

    // 获取实际占款天数和到期结算日
    *getBackInfo({ payload, callback }, { call, put }) {
      const type = payload.type;
      delete payload.type;
      const res = yield call(api.interBank.getBackInfo, payload);
      if (res && res.code == '0') {
        if (type) {
          //簿记转仓 使用 避免缓存getinfo 多个页面请求导致冲突
          callback(res.data);
          return;
        }
        yield put({
          type: 'save',
          payload: {
            getInfo: res.data,
          },
        });
        callback(res.data);
      } else {
        message.error('无数据');
      }
    },
    // 银行间质押式回购获取实际占款天数
    *getOccupationDays({ payload, callback }, { call, put }) {
      const res = yield call(api.interBank.getOccupationDays, payload);
      if (res) {
        callback(res);
      } else {
        message.error('获取实际占款天数失败');
      }
    },

    // 获取持仓信息
    *getPositionData({ payload, callback }, { call, put }) {
      const res = yield call(api.interBank.getPositionData, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error('无数据');
      }
    },

    // 指令下达
    *SaveInstruction({ payload, callback }, { call, put }) {
      const res = yield call(api.interBank.SaveInstruction, payload);
      if (res && res.success == true) {
        message.success('指令下达成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 保存簿记
    *SaveInstructionw2({ payload, callback }, { call, put }) {
      const res = yield call(api.interBank.SaveInstructionw2, payload);
      if (res && res.success == true) {
        message.success('操作成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 交收簿记
    *getTradeInstruction({ payload, callback }, { call, put }) {
      const res = yield call(api.interBank.getTradeInstruction, payload);
      if (res && res.success == true) {
        message.success('交收成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
    // 存续管理
    *tradeBookSave({ payload, callback }, { call, put }) {
      const res = yield call(api.fundTrade.tradeBookSave, payload);
      if (res && res.success == true) {
        message.success('操作成功');
        callback(res);
      } else {
        message.error(res.errorMsg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
